import { createStore } from 'vuex'
import router from '../route'
import { Video, Chapter, Course } from '../model/resource'
import { RouterData } from './util'
import { switchEmptyByData } from '../utils/utils'



// 创建一个新的 store 实例
const store = createStore({
    state() {
        return {
            router: {
                data: {}
            },
            router_data: new RouterData(),
            user: {
                account: 'root',
                password: 'root',
                token: '',
                setting: {
                    step: 'fps', // 'fps': 代表取视频的帧率, 其它取值
                    speed: 3,
                }
            },
            show: {
                menu: false,
                catalog: true,
                empty: false,
                backTab: false,
            }, 
            xgPlayer: {
                defaultPlaybackRate: 1.0,
                playNext: { // 接下来的播放列表
                    urlList: []
                },
                screenShot: {
                    saveImg: true, // 开启截图
                    quality: 0.92,
                    type: 'image/png',
                    format: '.png'
                }
            }
        }
    },
    mutations: {
        changeView(state, payloads) {
            router.push(payloads.name, payloads.params)
        },
        setToken(state, token) {
            state.user.token = token
        },
        async changeStage(state, params) {// params还要有历史数据
            if (params.path) {// 通过路径提交，也有新旧数据
                state.router_data.push(params.path, params.old_data, params.new_data)
                return
            }
            if (params.new_data) {
                // state.router.data = params.data
                state.router_data.push(params.name, params.old_data, params.new_data)
                return
            }
            let new_data = null
            let name = params.name
            console.log(name)
            if (!name) { // 没有name，说明params本身就是name字符串
                name = params
            }
            switch (name) {
                case 'video': {
                    new_data = (await new Video().gets()).videos;
                    switchEmptyByData(new_data) // 控制显示舞台的empty——view
                    state.router_data.push(name, params.old_data, new_data)
                    return;
                }
                case 'chapter': {
                    new_data = (await new Chapter().gets()).chapters;
                    switchEmptyByData(new_data)
                    state.router_data.push(name, params.old_data, new_data)
                    return;
                }
                case 'course': {
                    new_data = (await new Course().gets()).courses;
                    switchEmptyByData(new_data)
                    state.router_data.push(name, params.old_data, new_data)
                    return;
                }
            }
            // console.log('穿透',new_data)
            state.show.empty = false
            state.router_data.push(name, params.old_data, new_data)
        },
        // setRouterData(state, data){
        //     console.log('重置数据',data)
        //     state.router.data = data
        // },
        async entryPlayer(state, id) {
            data = (await new Video().get(id))
        },
        resetRouter(state) {
            state.router_data.clear()
        },
        showMenu(state) {
            state.show.menu = true
        },
        hiddenMenu(state) {
            state.show.menu = false
        },
        show(state, name) {
            state.show[name] = true
        },
        hidden(state, name) {
            state.show[name] = false
        },
    }
})


// 同步数据库中的配置
// await VSConfig.sync_remote_config()

export default store
